import sys
sys.path.append("../../")
from CoreSim.Unit import *

import numpy as np
import matplotlib.pyplot as plt
from math import ceil, floor, sqrt

'''
flow completion time(FCT)
Average FCTs
99th %-tile FCTs

(0,100KB]: Avg 
(0,100KB]: 99th Percentile 
(100KB,10MB]: Avg
(10MB,++): Avg
'''

plt.figure(figsize=(6,4))

fct_overall = []
fct_0KB_100KB = []
fct_100KB_10MB = []
fct_10MB_ = []
fct_tail99 = []

fct_overall_sum = 0
fct_0KB_100KB_sum = 0
fct_100KB_10MB_sum = 0
fct_10MB_sum = 0
fct_tail99_sum = 0

with open( "VL2_16ToRs_10Hosts_10Links_0.8Load_0.05Time.txt", 'r') as f:
    for line in f.readlines():
    	split = line.split()
    	flowSize_index = split.index('flowSize:')
    	fct_index = split.index('fct:')
    	flowSize = float(split[flowSize_index+1])
    	fct = float(split[fct_index+1])*1000 # ms

    	
    	fct_overall_sum += fct
    	fct_overall.append(fct)

    	if flowSize < 100*KB:
    		fct_0KB_100KB_sum += fct
    		fct_0KB_100KB.append(fct)
    	elif flowSize < 10*MB:
    		fct_100KB_10MB_sum += fct
    		fct_100KB_10MB.append(fct)
    	else:
    		fct_10MB_sum += fct
    		fct_10MB_.append(fct)
       
fct_overall.sort()
fct_0KB_100KB.sort()
fct_100KB_10MB.sort()
fct_10MB_.sort()

fct_tail99 = fct_overall[-len(fct_overall)/100+1:]
for x in fct_tail99:
	fct_tail99_sum += x


fct_overall_avg = fct_overall_sum/len(fct_overall)
fct_0KB_100KB_avg = fct_0KB_100KB_sum/len(fct_0KB_100KB)
fct_100KB_10MB_avg = fct_100KB_10MB_sum/len(fct_100KB_10MB)
fct_10MB_avg = fct_10MB_sum/len(fct_10MB_)
fct_tail99_avg = fct_tail99_sum/len(fct_tail99)

print 'fct_overall', len(fct_overall), fct_overall_sum, fct_overall_avg
print 'fct_0KB_100KB', len(fct_0KB_100KB), fct_0KB_100KB_sum, fct_0KB_100KB_avg
print 'fct_100KB_10MB', len(fct_100KB_10MB), fct_100KB_10MB_sum, fct_100KB_10MB_avg
print 'fct_10MB_', len(fct_10MB_), fct_10MB_sum, fct_10MB_avg
print 'fct_tail99', len(fct_tail99), fct_tail99_sum, fct_tail99_avg

'''
cdfx = fct_overall
cdfy = np.linspace(1 / len(fct_overall), 1.0, len(fct_overall))

fs = 14
plt.semilogx(cdfx, cdfy,'r', linewidth=1, label='Ideal')
plt.xlabel('FCT(ms)', fontsize=fs)
plt.ylabel('CDF', fontsize=fs)
plt.legend(loc=4, fontsize=fs)
plt.subplots_adjust(bottom=0.15, top=0.9, left=0.12, right=0.95)
plt.grid()
plt.show()
'''